import logging

from pylons import request, response, session, tmpl_context as c, url
from pylons.controllers.util import abort, redirect, redirect_to

from google.appengine.ext import db
from monkeytest.model import TestCase, TestSuite, IncrementalID

from monkeytest.lib.base import BaseController, render

log = logging.getLogger(__name__)

class TestcaseController(BaseController):
    def edit(self, key=None):
        if key != None:
            testcase = TestCase.get_by_id(key)
            c.key = key
            c.testsuite = str(testcase.testsuite.key())
            c.summary = testcase.summary
            c.instruction = testcase.instruction
            c.expected_result = testcase.expected_result
        else:
            c.key = None
            c.testsuite = None
            c.summary = None
            c.instruction = None
            c.expected_result = None
        
            if request.params.has_key("testsuite"):
                c.testsuite = request.params['testsuite']
        c.all_testsuites = TestSuite.select_list()
        return render('testcase/edit.html')
    
    def save(self, key=None):
        if key != None:
            testcase = TestCase.get_by_id(key)
        else:
            testcase = TestCase()
            testcase.id = IncrementalID.nextID()
        
        testcase.summary = request.params['summary']
        testcase.instruction = request.params['instruction']
        testcase.expected_result = request.params['expected_result']
        testcase.testsuite = db.Key(request.params['testsuite'])
        
        testcase.put()
        redirect_to("new_testcase", testsuite=str(testcase.testsuite.key()))
    